package SEApp.dialogs
{
	import UIFree.ObjectEvent;
	import UIFree.components.uif_window;
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;

	public class se_correction extends uif_window {
		private var _cy: int;
		private var _sb_brihtness: Object;
		private var _sb_contrast: Object;
		private var _sb_red: Object;
		private var _sb_green: Object;
		private var _sb_blue: Object;
		
		private var _lb_brihtness: Object;
		private var _lb_contrast: Object;
		private var _lb_colors: Object;
		
		public static const UPDATEMATRIX: String = 'UPDATEMATRIX';
		public function se_correction(parent:uif_component, ax:int=0, ay:int=0, a_styleClass: String = 'window') {
			super(parent, ax, ay, 230, 290, a_styleClass, true, 1);
			title.text = app.classes.data.CORRECTION;
			createDialog();
		}
		
		private function createDialog(): void {
			_cy = -30;
			_lb_brihtness 	= createLabel();
			_sb_brihtness 	= createScroll(updateMatrix);
			_lb_contrast 	= createLabel();
			_sb_contrast 	= createScroll(updateMatrix);
			_lb_colors 		= createLabel();
			_sb_red 		= createScroll(updateMatrix, 'sbRed');
			_sb_green 		= createScroll(updateMatrix, 'sbGreen');
			_sb_blue 		= createScroll(updateMatrix, 'sbBlue');
			var inner: Rectangle	= getInner();
			var resetButton: Object = new app.classes.button(this, inner.x + (inner.width - 80) / 2, inner.y + _cy + 33, 80, 30, 'button');
			resetButton.text = app.classes.data.RESET;
			resetButton.addEvent(MouseEvent.CLICK, reset);
			updateLabels();
		}
		
		protected function updateLabels(): void {
			_lb_brihtness.text 	= app.classes.data.BRIHTNESS + ' ' + _sb_brihtness.value.toString();
			_lb_contrast.text 	= app.classes.data.CONTRAST + ' ' + _sb_contrast.value.toString();
			_lb_colors.text		= app.classes.data.COLORBALANCE;
		} 
		
		protected function updateMatrix(e: Event = null): void {
			updateLabels();
			var br: Number 		= _sb_brihtness.value / 100;
			var con: Number 	= (100 + _sb_contrast.value) / 100;
			var red: Number 	= 1 + _sb_red.value / 100;
			var green: Number 	= 1 + _sb_green.value / 100;
			var blue: Number 	= 1 + _sb_blue.value / 100;

			var colors: Object 		= new Object();
			colors.blueMultiplier 	= con * blue;
			colors.greenMultiplier 	= con * green;
			colors.redMultiplier 	= con * red;
			colors.blueOffset 		= 0x7F * (1 - con) + 0xFF * br;
			colors.greenOffset 		= 0x7F * (1 - con) + 0xFF * br;
			colors.redOffset 		= 0x7F * (1 - con) + 0xFF * br;
			dispatchEvent(new ObjectEvent(UPDATEMATRIX, colors, true, true));
		}
		
		protected function reset(e: Event = null): void {
			_sb_brihtness.value = 0;
			_sb_contrast.value  = 0;
			_sb_red.value  		= 0;
			_sb_green.value  	= 0;
			_sb_blue.value  	= 0;
			updateLabels();
		}
		
		public function updateControls(colors: ColorTransform): void {
			_sb_brihtness.value 	= (colors.blueOffset + colors.greenOffset + colors.redOffset) / (3 * 0xFF) * 100;
			_sb_contrast.value		= 0;
			_sb_red.value			= colors.redOffset / 0xFF * 100;
			_sb_green.value			= colors.greenOffset / 0xFF * 100;
			_sb_blue.value			= colors.blueOffset / 0xFF * 100;
			updateLabels();
		}
		
		protected function createScroll(onValue: Function, a_styleClass: String = 'scrollbar'): Object {
			var inner: Rectangle = getInner();
			_cy += 33;
			return new app.classes.scrollbar100(this, inner.x, inner.y + _cy, inner.width, onValue, a_styleClass);			
		}
		
		protected function createLabel(caption: String=''): Object {
			var inner: Rectangle = getInner();
			_cy += 25;
			var label: Object = new app.classes.label(this, inner.x, inner.y + _cy + 5, inner.width, 25, 'label');
			label.text = caption;
			return label;
		}
	}
}